package assignmentonepenguins

import org.junit.Test

class CustomerIntegrationTests {
    @Test
    void test_UniqueEmailAddress() {
        // Create unique Customer1
        Customer customer1 = new Customer()
        customer1.setPassword('abc123')
        customer1.setEmail('abc123@gmail.com')
        customer1.save(flush: true)
        assert Customer.getCount() == 1
        assert customer1.validate()

        // Create unique Customer2
        Customer customer2 = new Customer()
        customer2.setPassword('123abc')
        customer2.setEmail('123abc@gmail.com')
        customer2.save(flush: true)
        assert Customer.getCount() == 2
        assert customer2.validate()

        // Create Customer 3 with a non-unique email address (C-2)
        Customer customer3 = new Customer()
        customer3.setPassword('abcdefg')
        customer3.setEmail('abc123@gmail.com')
        assert Customer.getCount() == 2
        assert !customer3.validate()
    }

    @Test
    void test_DeleteCustomer() {
        // Create unique Customer1
        Customer customer4 = new Customer()
        customer4.setPassword('abc123')
        customer4.setEmail('abc123@gmail.com')
        customer4.save(flush: true)
        assert Customer.getCount() == 1
        assert customer4.validate()

        // Create unique Customer2
        Customer customer5 = new Customer()
        customer5.setPassword('123abc')
        customer5.setEmail('123abc@gmail.com')
        customer5.save(flush: true)
        assert Customer.getCount() == 2
        assert customer5.validate()

        // Create a listing with Customer 2 as the seller
        Listing aListing = new Listing()
        aListing.setName('A Brand New Camera')
        aListing.setEndDate(new Date() + 7)
        aListing.setStartBid(149.99)
        aListing.setSeller(customer5)
        aListing.save(flush: true)

        // Delete Customer 4 which has no Listings or Biddings
        customer4.delete(flush: true)
        assert Customer.getCount() == 1

        try {
            // Attempt to delete Customer 5 which has a Listing
            customer5.delete(flush: true)
        }
        catch (Exception e) {
            // Verify that Customer 5 has not been deleted
            assert customer5.validate()
        }
        assert Customer.getCount() == 1
    }
}